package com.factory.test.activity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.factory.test.R;
import com.factory.test.base.BaseTestActivity;
import com.factory.test.databinding.ActivityBluetoothBinding;


/**
 * 蓝牙测试
 */

public class BluetoothActivity extends BaseTestActivity {

    private ActivityBluetoothBinding binding;
    private BluetoothAdapter bluetoothAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_bluetooth);

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();


        // 注册这个 BroadcastReceiver
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);                   //扫描到结果
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);     //扫描结束
        registerReceiver(mReceiver,intentFilter);

        if(bluetoothAdapter == null){
            // 设备不支持蓝牙功能。。。
            setDesc("设备不支持蓝牙功能");
        }else {
            scanDevices();
        }
    }


    int count = 0;
    // 创建一个接受 ACTION_FOUND 的 BroadcastReceiver
    private final BroadcastReceiver mReceiver = new BroadcastReceiver(){
        public void onReceive(Context context, Intent intent){
            String action = intent.getAction();
            // 当 Discovery 发现了一个设备
            if(BluetoothDevice.ACTION_FOUND.equals(action)){
                // 从 Intent 中获取发现的 BluetoothDevice
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // 将名字和地址放入要显示的适配器中
                //mArrayAdapter.add(device.getName + "\n" + device.getAddress());
                String name = device.getName();
                if(name != null){
                    binding.message.append(name + "\n" + device.getAddress() + "\n\n");
                }else{
                    binding.message.append(device.getAddress() + "\n\n");
                }
                count ++;
            }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){    //扫描结束
                setDesc("扫描到: " + count + " 个蓝牙设备");
            }
        }
    };


    // 在 onDestroy 中 unRegister

    private void scanDevices() {
        if(!bluetoothAdapter.isEnabled()){
            //android 10 已不能直接打开蓝牙了.
            if(bluetoothAdapter.enable()){
                Log.i("bt", "bluetoothAdapter.enable() is true");
                binding.message.setText("蓝牙开启中...\n");
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        binding.mac.setText("MAC: " + bluetoothAdapter.getAddress());
                        bluetoothAdapter.startDiscovery();
                        binding.message.setText("开始扫描蓝牙设备\n");
                    }
                }, 3000);
            }else{
                Log.i("bt", "bluetoothAdapter.enable() is false");
                binding.message.setText("自动开启蓝牙失败\n");
            }

//            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//            startActivityForResult(enableIntent, 1);
        }else{
            binding.message.setText("开始扫描蓝牙设备\n");
            bluetoothAdapter.startDiscovery();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1 ){
            if(resultCode == RESULT_OK){
                setDesc("蓝牙已开启");
                bluetoothAdapter.startDiscovery();
            }else {
                binding.message.setText("请开启蓝牙功能");
            }
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_bluetooth, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.bluetooth_settings){
            startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }
}